﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DCT.CodeBox.LightLog.Codes;
using DCT.CodeBox.ConfigParser.Codes;

namespace DCT.CodeBox.LightLog
{
    class Program
    {
        static void Main(string[] args)
        {
            AllLogTest alltest = new AllLogTest();

            alltest.ConsoleLogTest();
            alltest.ConsoleLogTest2();

            alltest.FileLogTest();

            alltest.MultiLogTest();

            Console.WriteLine("done");
        }
    }

    public class AllLogTest
    {
        private IConfigParser cp = new XmlConfigParser();

        public void ConsoleLogTest()
        {

            cp.Load(@"
<root>
    <val key=""level"">info,debug,warning</val> 
</root>");

            ConsoleLog cl = new ConsoleLog();
            cl.LoadConfig(cp);

            cl.D("test", "测试debug日志", new Exception("出现异常"));
            cl.I("test", "测试info日志", new Exception("出现异常"));
            cl.W("test", "测试waring日志", new Exception("出现异常"));
            cl.E("test", "测试error日志", new Exception("出现异常"));
            cl.F("test", "测试fatal日志", new Exception("出现异常"));
        }

        public void ConsoleLogTest2()
        {

            cp.Load(@"
<root>
    <val key=""level"">info,debug,warning</val> 
</root>");

            ConsoleLog cl = new ConsoleLog();
            cl.LoadConfig(cp);

            cl.D("test", new Dictionary<string, object>() { 
                {"a","aaa"},{"b","bbb"},{"c","ccc"},
            }, new Exception("出现异常"));
            cl.I("test", new { 
                Name = "Peter", Age = 20
            }, new Exception("出现异常"));            
        }

        public void FileLogTest()
        {
            cp.Load(@"
 <root>
        <!-- 日志级别，如果是空字符串或all，就所有级别都记录；如果需要过滤，则Debug,Info,Warning,Error,Fatal，用逗号隔开 -->
        <val key=""level"">all</val>
        <!-- 记录日志的根目录文件夹，如果是web支持""~/xx""的格式，支持相对路径和绝对路径 -->
        <val key=""folder"">log\</val>
        <!-- 日志文件格式，支持时间格式化，默认为yyyyMMdd.log -->
        <val key=""fileFormat"">""app_""yyyyMMdd"".log""</val>
        <!-- 日志缓冲写入条数，需要大于1 --> 
        <val key=""buffer"">9</val>
     </root>
");
            var fl = new FileLog();
            fl.LoadConfig(cp);

            for (int i = 0; i < 10; i++)
            {
                fl.D("test", "测试debug日志", new Exception("出现异常" + i.ToString()));
                fl.I("test", "测试info日志", new Exception("出现异常" + i.ToString()));
                fl.W("test", "测试waring日志", new Exception("出现异常" + i.ToString()));
                fl.E("test", "测试error日志", new Exception("出现异常" + i.ToString()));
                fl.F("test", "测试fatal日志", new Exception("出现异常" + i.ToString()));
            }

            //Console.WriteLine("not -- flush");
            Console.WriteLine("flush");

            fl.Flush();

        }

        public void MultiLogTest()
        {
            cp.Load(@"
        <root>
            <val key=""level"">info</val>
            <list key=""logs"">
                <object>
                    <val key=""name"">filelog</val>
                    <!-- 日志级别，如果是空字符串或all，就所有级别都记录；如果需要过滤，则Debug,Info,Warning,Error,Fatal，用逗号隔开 -->
                    <val key=""level"">all</val>
                    <!-- 记录日志的根目录文件夹，如果是web支持""~/xx""的格式，支持相对路径和绝对路径 -->
                    <val key=""folder"">log2\</val>
                    <!-- 日志文件格式，支持时间格式化，默认为yyyyMMdd.log -->
                    <val key=""fileFormat"">""app_""yyyyMMdd"".log""</val>
                    <!-- 日志缓冲写入条数，需要大于1 --> 
                    <val key=""buffer"">0</val>
                </object>
                <object>
                    <val key=""name"">mycl</val>
                    <val key=""level"">info,debug,warning</val> 
                </object>
            </list>
         </root>
");

            var ml = new MultiLog();
            ml.LoadConfig(cp);
            ml.SetMultiLogs(new Dictionary<string, ILog> { 
                {"filelog", new FileLog()},
                {"mycl", new ConsoleLog()}
            });

            for (int i = 0; i < 10; i++)
            {
                ml.D("test", "测试debug日志", new Exception("出现异常" + i.ToString()));
                ml.I("test", "测试info日志", new Exception("出现异常" + i.ToString()));
                ml.W("test", "测试waring日志", new Exception("出现异常" + i.ToString()));
                ml.E("test", "测试error日志", new Exception("出现异常" + i.ToString()));
                ml.F("test", "测试fatal日志", new Exception("出现异常" + i.ToString()));
            }
        }
    }

}
